function toggleSubmenu(header) {
  const menuGroup = header.parentElement;
  menuGroup.classList.toggle('collapsed');
}

// 页面加载时初始化菜单状态
document.addEventListener('DOMContentLoaded', function () {
  document.querySelectorAll('.menu-header').forEach((header) => {
    header.addEventListener('click', function () {
      toggleSubmenu(this);
    });
  });

  // 获取当前活动菜单项
  const activeItem = document.querySelector('.menu-item.active');
  if (activeItem) {
    // 展开包含活动菜单项的菜单组
    const menuGroup = activeItem.closest('.menu-group');
    if (menuGroup) {
      menuGroup.classList.remove('collapsed');
    }
  }

  // 默认折叠其他菜单组
  document.querySelectorAll('.menu-group').forEach((group) => {
    if (!group.querySelector('.menu-item.active')) {
      group.classList.add('collapsed');
    }
  });
});
